import axios from 'axios'
import { Message } from 'element-ui'

const request = axios.create({
    baseURL: "./",
    timeout: 5000
})

request.interceptors.request.use(config => {
    let headers = {"Content-Type":'application/json;charset=utf-8'};
    config.headers = {...headers, ...config.headers}
    return config
},
 error => {
     Message({
         message: error.message,
         type: 'error'
     });
     return Promise.reject(error);
});

request.interceptors.response.use(response => {
    let res = response.data;

    if(res.code == 200){
        return res.data;
    }else{
        Message({
            message: res.msg,
            type: 'error'
        });
        return Promise.reject(new Error(res.msg));
    }
},
    error => {
     Message({
         message: error.message,
         type: 'error'
     });
     return Promise.reject(error);
    }
)

export default request;